package First;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

import First.Window;

public class Look_Ask extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;

    JTable jtable;
    JScrollPane jscrollpane = new JScrollPane();

    Vector columnNames = null;
    Vector rowData = null;

    public Look_Ask(String num) {
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        columnNames = new Vector();//11项
        columnNames.add("学号");
        columnNames.add("课程号");
        columnNames.add("课程名");
        columnNames.add("成绩");
        rowData = new Vector();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                    "root",
                    "12345678");
            System.out.println(num);
            ps = conn.prepareStatement("SELECT SC.Sno,SC.Cno,Course.Cname,Grade FROM SC,Course WHERE SC.Sno='"+num+"' AND SC.Cno=Course.Cno ORDER BY SC.Sno");//      Student.Sno=SC.Sno AND
            //ps = conn.prepareStatement("SELECT * FROM Student");
            res = ps.executeQuery();
            while (res.next())
            {
                Vector hang = new Vector();
                hang.add(res.getString(1));
                hang.add(res.getString(2));
                hang.add(res.getString(3));
                hang.add(res.getString(4));
                rowData.add(hang);
            }
            System.out.println("load ok!");
        }catch (Exception q){
            q.printStackTrace();
            System.out.println("go die");
        }finally{
            try{
                res.close();
                ps.close();
                conn.close();
                System.out.println("close ok");
            }catch (SQLException o){
                o.printStackTrace();
                System.out.println("go die 2");
            }
        }

        jtable = new JTable(rowData,columnNames);
        jscrollpane = new JScrollPane(jtable);

        this.add(jscrollpane);
        this.setTitle("查询结果");
        this.setLayout(new GridLayout(2,7));
        this.add(jpforbutton);
        this.setLocation(300,300);
        this.setSize(800,300);
        this.setVisible(true);
        this.setResizable(false);
    }
}